<?php

 前后台用户表分离
 1.php artisan make:auth
 2.创建新的后台用户表
 php artisan make:migration create_admin_table --create=admin
 3.新建一个AdminAuth文件，复制Auth下的文件,修改命名空间
 4.修改$redirectTo = "/admin",登录成功后的地址
  添加一个新的protected  $guard = "admin",

  添加两个方法，用来展示登录表单，和注册表单

   
    public function username()
    {
        return 'username';
    }
    public function guard()
    {
        return Auth::guard('admin');
    }
  public function showLoginForm(){
     return view('admin.auth.login');
  }
  public function showRegistrationForm(){
     return view('admin.auth.register');
  }
  5.创建视图文件，在Views文件下，创建admin文件，复制auth文件到admin下
  6.路由
     Route::get('admin/login','AdminAuth\AuthController@showLoginForm');
     Route::post('admin/login','AdminAuth\AuthController@login');
     Route::get('admin/logout','AdminAuth\AuthController@logout');

    Route::get('admin/register','AdminAuth\AuthController@showRegistrationForm');
    Route::post('admin/register','AdminAuth\AuthController@register');
    //后台首页
    Route::get('/admin','AdminController@index');

    7.php artisan meke:controller AdminController.添加index方法

    8.php artisan meke:model Admin,声明表$table = 'admin'
      use Illuminate\Foundation\Auth\User as Authenticatable;
      让Admin继承Authenticatable

    9.在config文件的auth中，

          'guards' => [
          'web' => [
              'driver' => 'session',
              'provider' => 'users',
          ],

          'api' => [
              'driver' => 'token',
              'provider' => 'users',
          ],

          'admin'=>[
          'driver' => 'session',
          'provider' => 'admin',
        ]
      ],


        'providers' => [
                'users' => [
                    'driver' => 'eloquent',
                    'model' => App\User::class,
                ],

                // 'users' => [
                //     'driver' => 'database',
                //     'table' => 'users',
                // ],

                'admin'=>[
                  'driver' => 'eloquent',
                  'model' => App\Admin::class,
                ]
            ],


        'passwords' => [
            'users' => [
                'provider' => 'users',
                'table' => 'password_resets',
                'expire' => 60,
            ],


            'admin' => [
                'provider' => 'admin',
                'table' => 'password_resets',
                'expire' => 60,
            ],

        ],

    10.生成一个middleware来管理的是后台的登录路由，其他auth:admin管理其他页面，php  artisan make:middlware RedirectIfNotAdmin

         public function handle($request, Closure $next, $guard = 'admin'){
            //检测用户是否登录
             if (Auth::guard($guard)->check()) {
                 return redirect('/');
             }
             return $next($request);
         }

    11.在kernel.php 中注册，'admin'=> \App\Http\Middleware\RedirectIfNotAdmin::class

    12.在AdminController中使用RedirectIfNotAdmin
         public function __construct(){
           $this->middleware('admin');
         }



      eg:如何拿到后台用户的数据
      //获取用户所有数据
      Auth::guard('admin')->user();
      //检查用户是否登录
      Auth::guard('admin')->check();
      //清空用户登录信息
      Auth::guard('admin')->user()->logout();
